#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <ranges>
#include <Vector.hpp>

using namespace std;

class Solution
{
public:
    vector<int> solveQueries(vector<int> &nums, vector<int> &queries)
    {
        map<int, vector<int>> mp;
        for (int i = 0; i < nums.size(); i++)
        {
            mp[nums[i]].push_back(i);
        }
        for (int &q : queries)
        {
            if (mp.find(nums[q]) != mp.end() && mp[nums[q]].size() > 1)
            {
                vector<int> &v = mp[nums[q]];
                int index = lower_bound(v.begin(), v.end(), q) - v.begin();
                int disPrefix = (index == 0) ? q - v.back() + static_cast<int>(nums.size()) : q - v[index - 1];
                int disSubfix = (index == v.size() - 1) ? v[0] - q + static_cast<int>(nums.size()) : v[index + 1] - q;
                q = min(disPrefix, disSubfix);
            }
            else
                q = -1;
        }
        return queries;
    }
};

int main()
{
    cout << "Code3488" << endl;
    vector<int> nums = {1, 3, 1, 4, 1, 3, 2};
    vector<int> queries = {0, 3, 5};
    Solution s;
    auto res = s.solveQueries(nums, queries);
    PrintVector(res);
    return 0;
}